import { validateCompanyName , addObj ,getObj , putObj , validateUser , validateUserCreateBy } from '../api/assessindustrial'
import { getDict } from '~/utils'
import {console} from "vuedraggable/src/util/helper";
import CompList from "./CompList";
export default {
    name: "xinzeng",
    created(){
        this.rentStatuss = getDict('rentStatus')
        this.isTechnologys=getDict('isTechnology')
        this.assessResults=getDict('assess_industrial_result')
    },
    components: {CompList},
    data() {
        const validateCompanyNameTwo= (rule,value,callback)=>{
            let enheng1=this.assessInForm.companyName
            let enheng2=this.assessInForm.year
            if(this.isEdit!=undefined){
                callback()
            }else {
                validateCompanyName({"companyName": enheng1, "year": enheng2}).then(res => {

                    if (res.data.data != null) {
                        this.$message.error("年份与进驻公司名称的组合重复，请重新输入")
                        this.assessInForm.companyName = ''
                        this.assessInForm.year = ''
                    } else {
                        callback()
                    }
                })
            }
        };
        const isInt = (rule,value,callback)=>{
            if(value==''||value==undefined){
               callback()
            }else{
                const re = /^(0|[1-9][0-9]*)$/;
                const rsCheck = re.test(value);
                if (!rsCheck) {
                    callback(new Error('请输入0或大于0的整数'));
                } else {
                    if (value > 99999999999999) {
                        return callback(new Error("最大值不超过99999999999999"));
                    } else {
                        callback();
                    }
                }
            }
        };
        const doubleValidate = (rule, value, callback) => {
            if (!value) {
                callback();
            }
            let reg = /^(([1-9]\d*)|(0))(\.\d{1,2})?$/;
            if (!reg.test(value)) {
                return callback(new Error("数值格式错误（最多保留两位小数）"));
            } else {
                if (value > 9999999999999) {
                    return callback(new Error("最大值不超过9999999999999"));
                } else {
                    callback();
                }
            }
        };
            const doubleValidateTwo = (rule, value, callback) => {
                if (!value) {
                    callback();
                }
                let reg = /^([-]{0,1})(([1-9]\d*)|(0))(\.\d{1,2})?$/;
                if (!reg.test(value)) {
                    return callback(new Error("数值格式错误（最多保留两位小数）"));
                } else {
                    if (value > 9999999999999) {
                        return callback(new Error("最大值不超过9999999999999"));
                    } else {
                        callback();
                    }
                }
                ;
            };
            return {
                createByOne: '',
                isEdit: '',
                rentStatuss: [],
                isTechnologys: [],
                assessResult: [],
                user: [],
                assessInForm: {
                    companyName: '',
                    enterDesc: '',
                    registeredAddress: '',
                    introduceDept: '',
                    enterprise: '',
                    isListCompany: '',
                    parkName: '',
                    floor: '',
                    contractStartTm: '',
                    contractEndTm: '',
                    leaseTerm: '',
                    leaseArea: '',
                    rentPrice: '',
                    monthRent: '',
                    rentConcession: '',
                    rentStatus: '',
                    taxPlace: '',
                    statisticsLand: '',
                    totalTax: '',
                    taxGrowth: '',
                    yearOutput: '',
                    yearContribution: '',
                    lastYearRevenue: '',
                    revenueGrouth: '',
                    comprehensiveContribute: '',
                    talentNum: '',
                    applyProjectNum: '',
                    applyCopyrightNum: '',
                    applyPatentNum: '',
                    isTechnology: '',
                    isResearch: '',
                    researchInput: '',
                    researchGrouth: '',
                    researchRadio: '',
                    companyNum: '',
                    academicianNum: '',
                    year: '',
                    assessResult: '',
                    remarks: '',
                },
                assessInForm_rules: {
                    companyName: [
                        {required: true, message: "请输入进驻公司名称", trigger: "change"},
                    ],
                    enterDesc: [
                        {required: true, message: "请输入进驻情况", trigger: "blur"},
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    registeredAddress: [
                        {required: true, message: "请输入登记注册地", trigger: "blur"},
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    parkName: [
                        {required: true, message: "请输入进驻园区", trigger: "blur"},
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"},
                    ],
                    floor: [
                        {required: true, message: "请输入楼层", trigger: "blur"},
                    ],
                    leaseTerm: [
                        {validator: isInt, trigger: "blur"}
                    ],
                    rentConcession: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    taxPlace: [
                        // { validator: doubleValidate, trigger: "blur" }
                    ],
                    statisticsLand: [
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    totalTax: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    taxGrowth: [

                        {validator: doubleValidate, trigger: "blur"}
                        // { min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur" }
                    ],
                    yearOutput: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    yearContribution: [
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    lastYearRevenue: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    revenueGrouth: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    comprehensiveContribute: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    talentNum: [
                        {required: true, message: "请输入培养人数", trigger: "blur"},
                        {validator: isInt, trigger: "blur"}

                    ],
                    applyProjectNum: [
                        {required: true, message: "请输入申请个数", trigger: "blur"},
                        {validator: isInt, trigger: "blur"}

                    ],
                    applyCopyrightNum: [
                        {required: true, message: "请输入申请个数", trigger: "blur"},
                        {validator: isInt, trigger: "blur"}

                    ],
                    applyPatentNum: [

                        {validator: isInt, trigger: "blur"}
                        // { min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur" }
                    ],
                    isTechnology: [
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    isResearch: [
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    researchInput: [
                        {validator: doubleValidate, trigger: "blur"}
                    ],
                    researchGrouth: [
                        {validator: doubleValidateTwo, trigger: "blur"}
                    ],
                    researchRadio: [
                        {validator: doubleValidateTwo, trigger: "blur"}
                    ],
                    companyNum: [
                        {required: true, message: "请输入引入数量", trigger: "blur"},
                        {validator: isInt, trigger: "blur"}
                    ],
                    academicianNum: [
                        {required: true, message: "请输入引入数量", trigger: "blur"},
                        {validator: isInt, trigger: "blur"}
                    ],
                    year: [
                        {required: true, message: "请选择年度", trigger: "blur"},
                        {validator: validateCompanyNameTwo, trigger: "blur"}
                    ],
                    assessResult: [
                        {min: 0, max: 30, message: "长度在0-30个字符串", trigger: "blur"}
                    ],
                    remarks: [
                        {min: 0, max: 200, message: "长度在0-200个字符串", trigger: "blur"}
                    ],
                },
                choose: [
                    {
                        value: '',
                        label: '请选择',
                    },
                    {
                        value: '1',
                        label: '是',
                    },
                    {
                        value: '0',
                        label: '否',
                    },
                ],
            }
    },
    methods:{
        /*handleDialogClear：点击“取消”返回list页面*/
        handleDialogClear(){
            this.$router.push("/wy-exam/exam-industrial/list")
        },
        /*比较函数*/
        compare(date1, date2) {
            let dates1 = new Date(date1);
            let dates2 = new Date(date2);
            if (dates1 > dates2) {
                return true
            } else {
                return false
            }
        },


        /*保存函数*/
        handleDialogClick(){
            let enheng
            if(this.isEdit!=undefined){

                validateUserCreateBy(this.assessInForm.createBy).then(res=>{
                    if(res.data.data==this.user.user.deptId){
                        this.enheng=true
                    }else{
                        this.enheng=false
                    }
                })

            }
            if(this.enheng==true||this.enheng==undefined){
                this.$refs['assessInFormLand'].validate((valid)=>{
                    if(valid){
                        if(this.isEdit==undefined){
                            addObj(this.assessInForm).then(res=>{
                                this.$message.success("保存成功")
                                this.handleDialogClear()
                            }).catch(e=>{
                                this.$message.error("保存失败")
                            })
                        }else{
                            putObj(this.assessInForm).then( res =>{
                                this.$message.success("更新成功")
                                this.handleDialogClear()
                            } ).catch( e =>{
                                this.$message.error("更新失败")
                            })
                        }
                    }
                })
            }else{
                this.$message.error("您没有权限修改信息")
            }
        },
        chooseCName(){
                this.$refs.comp.show()
        },
        acceptList(data){
            this.assessInForm.companyName=data[0].customerName
            this.assessInForm.contractStartTm=data[0].startDate
            this.assessInForm.contractEndTm=data[0].endDate
            //计算租期（月）:此处只要未满一月按一月计算
            const year = new Date(data[0].endDate).getFullYear() - new Date(data[0].startDate).getFullYear()
            const month = new Date(data[0].endDate).getMonth() - new Date(data[0].startDate).getMonth()
            const day = new Date(data[0].endDate).getDate() - new Date(data[0].startDate).getDate()

            this.assessInForm.leaseTerm=year*12+month+(day<=0?0:1)
            this.assessInForm.leaseArea=data[0].buildingArea
            this.assessInForm.monthRent=data[0].rentAmount
            this.assessInForm.rentPrice=Math.ceil((this.assessInForm.monthRent/this.assessInForm.leaseArea)*100)/100
            // this.assessInForm.=data[0].contractName
        },
    },
    mounted(){
        // SecurityUtils.getCurrentUser().getUser().getUserId();
        this.isEdit=this.$route.params.isEdit
        if(this.isEdit!=undefined){
            getObj(this.isEdit).then(res=>{
                this.assessInForm=res.data.data
            })
        }
        validateUser().then((res)=>{
            this.user=res.data.data
            if(this.isEdit==undefined){
                this.assessInForm.introduceDept=this.user.deptName
            }
        })
    },
}
